package com.example.demo53.methods.net;


import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class boardcast_ip  extends Thread{
    private  String IP;//boardcast ip
    private  String route_ip;
    private int port1 ;

    private String code;
    private String msg;
    private String SSID;
    private  int color;
    //byte[] receiveMsg ;
    private boolean send = true;
public void initdata_ip(String a ,int b, String c,String ssid){
    route_ip=a;
    port1 =b;
    code=c;
    SSID = ssid;
//    msg = a+p+c;
}

public void initdata_color(int a){
    color = a;
}
    @Override
    public void run() {
        try {

            IP = NetWorkUtil.getBroadcast();
            // 可删除 测试用
            if(IP == null || IP.length() == 0)
            {
                IP="192.168.2.124";//
            }
            if(route_ip==null)
            {
                route_ip="null_route_ip";
            }
            if(code==null){
              code = "00000000";
            }
            //用来定义灯具的KIND（不同KIND配对不同控制界面
//            if(color==0){
//                code = "123";
//            }
            if(port1==0){
                port1 = 8080;
            }
            if (SSID==null){
                SSID = " ";
            }
           msg = route_ip+" "+port1+" "+code+" "+SSID;

                byte[] receiveMsg = msg.getBytes();

                DatagramPacket receivePacket = new DatagramPacket(receiveMsg,
                        receiveMsg.length,
                        InetAddress.getByName(IP), //目标地址
                        port1);      //广播端口


                DatagramSocket socket = new DatagramSocket();
                socket.send(receivePacket);

                socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//--------------已废弃
    public synchronized void putMsg() {
        // 唤醒线程
       if (msg.length()== 0)
            notify();
        //receiveMsg =msg.getBytes();
    }




}